﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deflection
{
    class TestEnemy2 : Enemy
    {
        public static Texture2D EnemySprite { get; set; }

        float RotationAngle = 0.0f;

        private List<FireTest> fireTests = new List<FireTest>();

        public TestEnemy2(Vector2 position) : base(position, null, Color.White)
        {
            //this.movementTarget = CreateMovementTarget();
            Type = EnemyType.SquareEnemy;
            // Create a path for it to follow
            path = new Path();
            path.Nodes = Paths.Circle;
            path.position = new Vector2(840, 360); // screen center
            path.scale = 1.0f;

            // Makes the enemy go to the first path node when it spawns
            this.movementTarget = path.position + (path.GetCurrentPositionOnPath() * path.scale);


            //fireTests.Add(new FireTest(0));
            //fireTests.Add(new FireTest(5));
            //fireTests.Add(new FireTest(7));
        }

        public TestEnemy2(Vector2 position, Path path) : base(position, null, Color.White)
        {
            Type = EnemyType.SquareEnemy;
            this.movementTarget = CreateMovementTarget();
         
            if (!path.CheckPathIsValid())
            {
                Console.WriteLine("INVALID PATH");
            }
            else
            {
                this.path = path;
            }

            //this.color = color;
        }

        public override void Update(GameTime gameTime)
        {
            if (GetDistanceToMovementTarget() <= 4)
            {
                // Enemy is 'on' the target so create a new one
                // Check if this enemy has been given a path to follow
                if (HasPath())
                {
                    // give it a move target to go to
                    this.movementTarget = path.position + (path.GetCurrentPositionOnPath(pathNodeIndex) * path.scale);

                    // Get the next nodeIndex of the path
                    pathNodeIndex = path.Next(pathNodeIndex);

                }
                else
                {
                    this.movementTarget = CreateMovementTarget();
                    
                }
            }

            // Get the direction to the move target
            FindDirection(this.movementTarget); 
            // Finally make the enemy move
            Move();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (alive)
            {

                // Rotation speed
                //        < 1: slower
                //        > 1: faster
                //float rotationSpeed = 7;            

                // The time since Update was called last.
                //float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

                // Calculate rotation angle based on time elapsed
                //float circle = MathHelper.Pi * 2;

                /*RotationAngle = 0;//+= (elapsed * rotationSpeed) % circle;
                Random r = new Random();
                position.X -= 1;*/ // r.Next(0, 2);

                // The Enemy
                spriteBatch.Draw(TestEnemy2.EnemySprite, position, null, Color.OrangeRed, RotationAngle, new Vector2(TestEnemy2.EnemySprite.Bounds.Center.X, TestEnemy2.EnemySprite.Bounds.Center.Y), 1, SpriteEffects.None, 0);
                // It's Movement Target
                spriteBatch.Draw(TestEnemy2.EnemySprite, movementTarget, null, Color.Green, RotationAngle, new Vector2(TestEnemy2.EnemySprite.Bounds.Center.X, TestEnemy2.EnemySprite.Bounds.Center.Y), 0.25f, SpriteEffects.None, 0);
                // It's Path's Center
                spriteBatch.Draw(TestEnemy2.EnemySprite, path.position, null, Color.Purple, RotationAngle, new Vector2(TestEnemy2.EnemySprite.Bounds.Center.X, TestEnemy2.EnemySprite.Bounds.Center.Y), 0.25f, SpriteEffects.None, 0);
            }
         
        }

        public override void Move()
        {           
            // could use a Sin or Cos wave :)

            this.Position = Vector2.Add(this.Position, this.direction); 
            
        }

        /// <summary>
        /// Handles all the firing for the enemy
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Fire(GameTime gameTime)
        {
            // fire when enemy is at a specific at path node index
            /*float dis = GetDistance(Position, path.position + (path.Nodes[6] * path.scale));

            if (dis <= 1)
            {
                ProjectileFactory.CreateProjectile(this.Position, Projectiles.Projectile.ProjectileType.Missile);
                Console.WriteLine(dis);
            }*/

            /*if (!fired)
            {
                if (path.CurrentNodeIndex == fireAtIndex)
                {
                    ProjectileFactory.CreateProjectile(this.Position, Projectiles.Projectile.ProjectileType.Missile);
                    fired = true;
                }
            }
            else
            {
                if (path.CurrentNodeIndex != fireAtIndex)
                {
                    fired = false;
                }
            }*/

            /*foreach (FireTest test in fireTests)
            {
                if (test.Check(path.CurrentNodeIndex - 1)) // !!! Must have  '-1'
                {
                    ProjectileFactory.CreateProjectile(Position, Projectiles.Projectile.ProjectileType.Missile);
                    Console.WriteLine("Fire " + (path.CurrentNodeIndex - 1));
                }
            }*/
            
        }



        

    }

    public class FireTest
    {

        public bool Fired { get; set; }

        public int FireIndex { get; set; }

        public FireTest(int fireIndex)
        {
            Fired = false;
            FireIndex = fireIndex;
        }

        public bool Check(int currentIndex)
        {
            if (!Fired)
            {
                if (currentIndex == FireIndex)
                {
                    Fired = true;
                    return true;
                }
            }
            else
            {
                if (currentIndex != FireIndex)
                {
                    Fired = false;
                    return false;
                }
            }

            return false;
        }

        public void Check(Vector2 postion)
        {

        }

    }
}
